package com.kornero.geek.activity.game;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.webkit.WebView;
import com.kornero.common.app.GoogleAdActivity;
import com.kornero.common.util.HandlerUtil;
import com.kornero.common.util.MessageUtil;
import com.kornero.geek.R;
import com.kornero.geek.core.Timer;
import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.Observable;
import java.util.Observer;

import static com.kornero.common.util.ActivityUtil.getPreferencesName;
import static com.kornero.common.util.ActivityUtil.getTag;
import static com.kornero.common.util.CalendarUtil.getCalendar;

public class MainActivity extends GoogleAdActivity {

    public static final String PLAYER_NAME = "player.name";
    public static final String NEW_GAME_FLAG = "new.game.flag";

    public static final String PREFERENCES_NAME = getPreferencesName(MainActivity.class);
    public static final String PREFERENCE_CALENDAR = "calendar";

    private static final String TAG = getTag(MainActivity.class);
    private static final Timer TIMER = new Timer(1000 / 60); // One hour per second.
    private static final long YEAR2K = 946684800000L;

    private Handler timerHandler;

    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_main);

        final String playerName = getExtraString(savedInstanceState, PLAYER_NAME);
        Log.d(TAG, "Start game for: " + playerName);

        // Save player name.
        setPreferenceString(PREFERENCES_NAME, PLAYER_NAME, playerName);

        if (StringUtils.isNotBlank(getExtraString(savedInstanceState, NEW_GAME_FLAG))) {
            startNewGame();
        }

        // Turn on Google Ads.
        setUpAd();

        bind();

        timerSetUp(getPreferenceLong(PREFERENCES_NAME, PREFERENCE_CALENDAR));
    }

    @Override
    protected void onPause() {
        TIMER.stop();

        // Save time.
        setPreferenceLong(PREFERENCES_NAME, PREFERENCE_CALENDAR, TIMER.getCalendar().getTimeInMillis());

        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Load time.
        final long time = getPreferenceLong(PREFERENCES_NAME, PREFERENCE_CALENDAR);
        TIMER.startFrom(getCalendar(time));
    }

    private void bind() {
        timerHandler = HandlerUtil.valueOf(findTextViewById(R.id.textView10));
    }

    private void timerSetUp(final long time) {
        TIMER.addObserver(new Observer() {
            @Override
            public void update(Observable observable, Object data) {
                if (data instanceof Calendar) {
                    final Calendar calendar = (Calendar) data;
                    timerHandler.sendMessage(MessageUtil.valueOf(calendar));
                }
            }
        });

        TIMER.startFrom(getCalendar(time));
    }

    private void startNewGame() {
        setPreferenceLong(PREFERENCES_NAME, PREFERENCE_CALENDAR, YEAR2K);

        final WebView webView = new WebView(this);
        webView.loadData(getString(R.string.example_text), "text/html", "utf-8");
        webView.getSettings().setDefaultTextEncodingName("utf-8");

        new AlertDialog.Builder(this).setCancelable(false).setView(webView)
                .setNeutralButton("Okay", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).show();
    }
}